package cn.ococci.dm.Main.Adapter.Base;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.ococci.dm.Base.BaseApp;
import cn.ococci.dm.Main.Tools.ImageTool;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/9/23.
 * 这里就相当与BaseAdapter中直接写的ViewHolder
 */
public class BaseViewHolder {

    private Context mContext;
    private ViewGroup parent;
    private int layoutId;
    private int position;

    private View convertView;  //生成单个的视图

    private final SparseArray<View> storageViews;  //存储视图

    private Object relationsObj;  //实现获知数据改变的根本

    /**
     * 只有当获取的convertView is null 才会去init
     * @param c
     * @param parent
     * @param layoutId
     * @param position
     */
    protected BaseViewHolder(Context c, ViewGroup parent, int layoutId,int position){

        this.mContext = c;
        this.parent = parent;
        this.layoutId = layoutId;
        this.position = position;

        this.storageViews = new SparseArray<>();

        this.convertView = LayoutInflater.from(mContext).inflate(this.layoutId,this.parent,false);
        this.convertView.setTag(this);
    }

    /**
     * 获取到convertView
     * @param c
     * @param parent
     * @param cv
     * @param layoutId
     * @param position
     * @return
     */
    static BaseViewHolder obtain(Context c,  View cv, ViewGroup parent,int layoutId, int position){
        if(cv == null){
            return  new BaseViewHolder(c, parent, layoutId, position);
        }

        BaseViewHolder holder = (BaseViewHolder) cv.getTag();
        if(holder.layoutId != layoutId){
            return  new BaseViewHolder(c, parent, layoutId, position);
        }
        holder.position = position;

        return holder;
    }

    protected <T extends View> T findView(int viewId){

        View view = storageViews.get(viewId);
        if (view == null) {
            view = convertView.findViewById(viewId);
            storageViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView(){
        return convertView;
    }

    public  void setRelationsObject(Object obj){
        this.relationsObj = obj;
    }

    public int getLayoutId() {
        return layoutId;
    }

    public  Object getRelationsObject(){
        return relationsObj;
    }

    //××××××××××××××××××××××××××××（获取到相应的控件并且设置视图的显示效果）×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××


    /**
     * 设置文本信息
     * @param viewId
     * @param content
     * @return
     */
    public BaseViewHolder setTextView(int viewId, String content){
        TextView tv = findView(viewId);
        tv.setVisibility(View.VISIBLE);
        tv.setText(content);
        return  this;
    }

    /**
     * 设置文本信息
     * @param viewId
     * @param show 是否显示
     * @return
     */
    public BaseViewHolder setTextView(int viewId,boolean show){
        TextView tv = findView(viewId);
        tv.setVisibility(show ? View.VISIBLE : View.GONE);
        return  this;
    }



    /**
     * 设置文本信息
     * @param viewId
     * @param show 是否显示
     * @return
     */
    public BaseViewHolder setTextView(int viewId,String charSequence,boolean show){
        TextView tv = findView(viewId);
        tv.setText(charSequence);
        tv.setVisibility(show ? View.VISIBLE : View.GONE);
        return  this;
    }



    /**
     * 设置文本信息
     * @param viewId
     * @param show 是否显示
     * @return
     */
    public BaseViewHolder setTextView(int viewId, boolean show, View.OnClickListener listener){
        TextView tv = findView(viewId);
        tv.setVisibility(show ? View.VISIBLE : View.GONE);
        tv.setOnClickListener(listener);
        return  this;
    }


    /**
     * 设置文本信息和监听
     * @param viewId
     * @param content
     * @param listener
     * @return
     */
    public BaseViewHolder setTextView(int viewId, CharSequence content,View.OnClickListener  listener){
        TextView tv = findView(viewId);
        tv.setText(content);
        tv.setOnClickListener(listener);
        return  this;
    }




    /**
     * 设置文本信息和监听
     * @param viewId
     * @param content
     * @param listener
     * @return
     */
    public BaseViewHolder setTextView(int viewId, CharSequence content,boolean show,View.OnClickListener  listener){
        TextView tv = findView(viewId);
        tv.setText(content);
        tv.setVisibility(show ? View.VISIBLE : View.GONE);
        tv.setOnClickListener(listener);
        return  this;
    }





    /**
     * 设置图片
     * @param viewId
     * @param resId
     * @return
     */
    public BaseViewHolder setImageView(int viewId, int resId,View.OnClickListener listener){
        ImageView iv = findView(viewId);
        iv.setBackgroundResource(resId);
        iv.setOnClickListener(listener);
        //这里可以根据自己的需求去设置
        return this;
    }




    /**
     * 设置图片
     * @param viewId
     * @param resId
     * @return
     */
    public BaseViewHolder setImageView(int viewId, int resId){
        ImageView iv = findView(viewId);
        iv.setBackgroundResource(resId);
        //这里可以根据自己的需求去设置
        return this;
    }


    /**
     * 设置ImageView的可见性
     * @param viewId
     * @param show
     * @return
     */
    public BaseViewHolder setImageView(int viewId, boolean show){
        ImageView iv = findView(viewId);
        iv.setVisibility(show ? View.VISIBLE : View.GONE);
        return this;
    }


    public BaseViewHolder setImageView(int viewId, Bitmap bitmap){

        ImageView iv = findView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }


    /**
     * 设置图片的监听器
     * @param viewId
     * @param listener
     * @return
     */
    public BaseViewHolder setImageView(int viewId,boolean show,View.OnClickListener listener){
        ImageView iv = findView(viewId);
        iv.setVisibility(show?View.VISIBLE:View.GONE);
        iv.setOnClickListener(listener);

        return this;
    }


    public BaseViewHolder setImageView(int viewId, Bitmap bitmap, boolean show, View.OnClickListener listener){

        ImageView iv = findView(viewId);
        iv.setVisibility(show?View.VISIBLE:View.GONE);
        iv.setImageBitmap(bitmap);
        iv.setOnClickListener(listener);
        return this;
    }








    /**
     * 设置视图的可见性
     * @param viewId
     * @param show
     * @return
     */
    public BaseViewHolder setViewStatus(int viewId,boolean show){
        View  view = findView(viewId);
        view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        return this;
    }


    /**
     * 设置视图的背景颜色
     * @param viewId 视图id
     * @param color  颜色
     * @return
     */
    public BaseViewHolder setBackground(int viewId,int color){
        View view = findView(viewId);
        view.setBackgroundColor(color);
        return this;
    }


}
